<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once BASE_PATH . 'application/controllers/base' . EXT;

class Login extends Base {

    public function __construct() {
        parent::__construct();
        $this->load->model('suscriptoresDAO');
        $this->load->model('sessionDAO');
    }

    public function index() {

        $email = $this->input->post('email');
        $pass = $this->input->post('pass');

        if (!empty($email) and !empty($pass)) {
            if ($this->suscriptoresDAO->validateUser(array(
                        'email' => $this->input->post('email'),
                        'pass' => $this->input->post('pass'))
            )) {

                $user = $this->suscriptoresDAO->findUserByEmail($this->input->post('email'));
                $this->sessionDAO->setUserId($user->id);
                //echo "{success:true}";
                echo json_encode(array('nLogin' => TRUE));
            }else
                echo json_encode(array('nLogin' => FALSE, 'responseText' => 'Correo o Contraseña no válidos'));
            //echo "{success:false,responseText:'Usuario no valido'}";
        }else
            echo json_encode(array('nLogin' => FALSE, 'responseText' => 'Llena todos los campos'));
        //echo "{success:false,responseText: 'Llena los campos'}";
    }

    public function logout() {
        $this->sessionDAO->logOut();
        redirect('/');
        //echo json_encode(array('oLogin' => TRUE));
    }

}